import { Component, OnInit } from '@angular/core';
import { Observable } from "rxjs/Observable";

import { DicKeyEditComponent } from "../dic-key-edit/dic-key-edit.component";
import { DicValueListComponent } from "../dic-value-list/dic-value-list.component";

import { BladeContainerBase } from "../../../../shared/blade/blade-container-base";
import { BladeData } from "../../../../shared/blade/blade-data";
import { Blade } from "../../../../shared/blade/blade.model";


import { Command } from "../../../../shared/toolbar.service";
import { Setofkey } from "../../../../entity/setofkey.model";
import { SetofkeyService } from "../../../../service/setofkey.service";
import { DialogResult } from "../../../../shared/dialog.service";

@Component({
    selector: 'app-dic-key-list',
    templateUrl: './dic-key-list.component.html',
    styleUrls: ['./dic-key-list.component.css']
})
export class DicKeyListComponent extends BladeContainerBase<Setofkey>{
    setofkeys: Setofkey[] = [];
    original: Setofkey[] = [];
    isAllChecked: boolean = false;
    sreachKeyCode: string = "";

    constructor(private setofkeyService: SetofkeyService) {
        super();
    }
    ngOnInit(): void {
        this.commands = [
            new Command("add", "添加", "fa fa-plus", "setofkey:insert", blade => this.exeAdd(blade), blade => this.canExeAdd(blade)),
            new Command("edit", "编辑", "fa fa-edit", "setofkey:update", blade => this.exeEdit(blade), blade => this.canExeEdit(blade)),
            new Command("delete", "删除", "fa fa-trash-o", "setofkey:delete", blade => this.exeDelete(blade), blade => this.canDelete(blade))
        ];
        super.ngOnInit();
    }
    /**
     * 初始化数据
     * 
     * 
     * @memberof DicKeyListComponent
     */
    initData(): any | Observable<any> | Promise<any> {
        this.isAllChecked = false;
        this.setofkeyService
            .getSetofkeys()
            .then(res => {
                if (!res) { return false };
                this.original = [...res];
                this.onSearch(this.sreachKeyCode);
            })
            .catch(error => this.bladeData.error = error)
            .then(res => this.bladeData.isLoading = false);
    }
    exeAdd(blend: Blade): void {
        let data: BladeData = new BladeData();
        data.id = "dicKeyCreate";
        data.title = "数据字典键";
        data.subtitle = "创建一个数据字典键";
        data.isClosingDisabled = false;
        let createKeyBlade = this.bladeService.createBlade(DicKeyEditComponent, data, true);
        this.bladeService.showBlade(createKeyBlade, this.currentBlade);
    }
    canExeAdd(blend: Blade): boolean {
        return true;
    }
    exeEdit(blend: Blade): void {
        if (this.currentEntity != undefined) {
            let data: BladeData = new BladeData();
            data.id = "dicKeyEdit";
            data.title = this.currentEntity.keyCode;
            data.subtitle = this.currentEntity.keyDesc
            let editKeyBlade = this.bladeService.createBlade(DicKeyEditComponent, data, false, this.currentEntity);
            this.currentBlade.bladeService.showBlade(editKeyBlade, this.currentBlade);
        }
    }
    canExeEdit(blend?: Blade): boolean {
        return this.currentEntity != null;
    }
    exeDelete(blend: Blade): void {
        let selectedKeys = this.getSelected();
        if (selectedKeys.length > 0) {
            this.dialogService
                .showConfirmationDialog("确认删除", "是否要删除选中项？")
                .subscribe(dialogResult => {
                    if (dialogResult == DialogResult.Ok) {
                        this.currentBlade.bladeData.isLoading = true;
                        this.setofkeyService.batchDelete(selectedKeys.map(o => o.keyId))
                            .then(res => {
                                if (res) {
                                    this.refresh(undefined);
                                }
                            })
                            .catch(error => this.bladeData.error = error)
                            .then(res => this.bladeData.isLoading = false);
                    }
                })

        }

    }
    canDelete(blend?: Blade): boolean {
        return this.getSelected().length > 0;
    }
    /**
     * 查询过滤
     * 
     * @param {string} keyCode 
     * 
     * @memberof DicKeyListComponent
     */
    onSearch(keyCode: string) {
        this.sreachKeyCode = keyCode;
        this.isAllChecked = false;
        this.original.forEach(o => o.isChecked = false);
        keyCode = keyCode.toLowerCase().trim();
        this.setofkeys = [];
        this.setofkeys = keyCode == "" ? [...this.original]
            : [...this.original.filter(o => o.keyCode.toLowerCase().startsWith(keyCode))];
        this.currentEntity = null;
        this.canExeEdit();
        this.canDelete();
    }
    /**
     * 单选
     * 
     * @param {any} event 
     * @param {any} key 
     * 
     * @memberof DicKeyListComponent
     */
    onItemCheack(event: HTMLInputElement, key: Setofkey) {
        key.isChecked = event.checked;
    }
    /**
     * 全选/全不选
     * 
     * @param {any} event 
     * 
     * @memberof DicKeyListComponent
     */
    onAllCheack(event: HTMLInputElement) {
        if (this.setofkeys.length > 0) {
            for (let i = 0; i < this.setofkeys.length; i++) {
                this.setofkeys[i].isChecked = event.checked;
            }
        }
    }
    onSeletedChange(selectedKey: Setofkey) {
        this.showDicValueListBlade(selectedKey);
    }
    /**
     * 展开数据字典值列表弹出层
     * 
     * @param {Setofkey} selectedKey 
     * 
     * @memberof DicKeyListComponent
     */
    showDicValueListBlade(selectedKey: Setofkey) {
        if (this.currentEntity != selectedKey
            || this.bladeService.findBlade("dicValueList") == undefined) {
            let data: BladeData = new BladeData();
            data.id = "dicValueList";
            data.title = selectedKey.keyCode + "值列表";
            data.subtitle = selectedKey.keyDesc;
            data.isClosingDisabled = false;
            let dicValueListBlade = this.bladeService.createBlade(DicValueListComponent, data, false, null, selectedKey);
            this.bladeService.showBlade(dicValueListBlade, this.currentBlade).then(res => {
                if (res) {
                    this.currentEntity = selectedKey;
                }
            });
        }
    }
    /**
     * 获得选中项
     * 
     * @returns {Setofkey[]} 
     * 
     * @memberof DicKeyListComponent
     */
    getSelected(): Setofkey[] {
        if (this.setofkeys.length > 0) {
            let filter = this.setofkeys.filter(ref => ref.isChecked)
            return filter == null ? [] : filter;
        }
        return [];
    }
}




// WEBPACK FOOTER //
// ./src/app/module/basic/dic/dic-key-list/dic-key-list.component.ts